﻿Ext.define("PSI.Dispatcher.MainForm", {
    createToolButtons: function () {
        var me = this;
        var result = [];
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                location.replace("/");
            }
        });
        return result;
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "调度台账",
            tbar: me.createToolButtons(),
            items: [{
                region: "north", xtype: "container",
                height: 70,
                layout: {
                    type: "table",
                    columns: 8
                }, border: false,
                items: [{
                    xtype: "label",
                    text: "调度"
                }, {
                    xtype: "psi_bc_dispatcherfield",
                    margin: "4 5 0 5",
                    id: "editDispatcherQC",
                    width: 170
                }, {
                    xtype: "label",
                    text: "拖车公司"
                }, {
                    xtype: "psi_truck_truckcompanyfield",
                    margin: "4 5 0 5",
                    id: "editTruckCompanyQC",
                    width: 170
                }, {
                    xtype: "label",
                    text: "车号"
                }, {
                    xtype: "psi_truck_trucknumberfield",
                    id: "editTruckNumberQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "司机"
                }, {
                    xtype: "textfield",
                    id: "editDriverQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "派车日期"
                }, {
                    xtype: "datefield",
                    margin: "4 5 0 5",
                    format: "Y-m-d",
                    width: 170,
                    id: "editPlanDT1QC",
                    value: new Date()
                }, {
                    xtype: "label",
                    text: "----"
                }, {
                    xtype: "datefield",
                    margin: "4 5 0 5",
                    format: "Y-m-d",
                    width: 170,
                    id: "editPlanDT2QC",
                    value: new Date()
                }, {
                    xtype: "container",
                    border: 0,
                    layout: "hbox",
                    items: [
                        {
                            xtype: "button",
                            text: "查询",
                            iconCls: "psi-button-refresh",
                            margin: "4 5 0 5",
                            width: 90,
                            handler: me.onRefreshPlanGird,
                            scope: me
                        }, {
                            xtype: "button",
                            text: "清空查询条件",
                            margin: "4 5 0 5",
                            handler: me.onClearQC,
                            scope: me
                        }
                    ],
                    colspan: 2
                }
                ]
            }, {
                region: "center", xtype: "container", layout: "fit", border: false,
                items: [me.getPlanGrid()]
            }
            ]
        });

        me.panel = panel;

        Ext.getCmp("editTruckNumberQC").setLinkedEditor({
            truckCompanyEditor: Ext.getCmp("editTruckCompanyQC")
        });
    },

    getMainPanel: function () {
        return this.panel;
    },

    getPlanGrid: function () {
        var me = this;

        if (!me.__PlanGrid) {
            me.__PlanGrid = me.createPlanGrid();
        }

        return me.__PlanGrid;
    },

    createPlanGrid: function () {
        var modelName = "PSIDispatcherListData";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "tripNumber", "truckNumber", "inAddress", "inDT1Plan",
                "inDT1Driver", "inDT4Plan", "inDT4Driver", "outAddress",
                "outDT1Plan", "outDT1Driver", "outDT4Plan", "outDT4Driver",
                "inStatus", "outStatus", "inTruckDT", "outTruckDT"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    { header: "趟次号", dataIndex: "tripNumber", menuDisabled: true, locked: true },
                    { header: "车号/司机/拖车公司", dataIndex: "truckNumber", menuDisabled: true, width: 150, locked: true },
                    {
                        header: "进口", menuDisabled: true,
                        columns: [
                            { header: "门点", dataIndex: "inAddress", menuDisabled: true },
                            { header: "派车时间", dataIndex: "inTruckDT", menuDisabled: true, width: 120 },
                            { header: "计划提箱时间", dataIndex: "inDT1Plan", menuDisabled: true, width: 120 },
                            { header: "实际提箱时间", dataIndex: "inDT1Driver", menuDisabled: true, width: 120 },
                            { header: "计划完成时间", dataIndex: "inDT4Plan", menuDisabled: true, width: 120 },
                            { header: "实际完成时间", dataIndex: "inDT4Driver", menuDisabled: true, width: 120 },
                            { header: "状态", dataIndex: "inStatus", menuDisabled: true, width: 80 }
                        ]
                    },
                    {
                        header: "出口", menuDisabled: true,
                        columns: [
                            { header: "门点", dataIndex: "outAddress", menuDisabled: true },
                            { header: "派车时间", dataIndex: "outTruckDT", menuDisabled: true, width: 120 },
                            { header: "计划装箱时间", dataIndex: "outDT1Plan", menuDisabled: true, width: 120 },
                            { header: "实际装箱时间", dataIndex: "outDT1Driver", menuDisabled: true, width: 120 },
                            { header: "计划完成时间", dataIndex: "outDT4Plan", menuDisabled: true, width: 120 },
                            { header: "实际完成时间", dataIndex: "outDT4Driver", menuDisabled: true, width: 120 },
                            { header: "状态", dataIndex: "outStatus", menuDisabled: true, width: 80 }
                        ]
                    }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    refreshPlanGrid: function (selectedId) {
        var me = this;
        var grid = me.getPlanGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }
        var params = {
            dispatcher: Ext.getCmp("editDispatcherQC").getValue(),
            truckCompany: Ext.getCmp("editTruckCompanyQC").getValue(),
            truckNumber: Ext.getCmp("editTruckNumberQC").getValue(),
            driver: Ext.getCmp("editDriverQC").getValue(),
            planDT1: Ext.getCmp("editPlanDT1QC").getValue(),
            planDT2: Ext.getCmp("editPlanDT2QC").getValue()
        };
        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/Dispatcher/GetListData",
            params: params,
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onRefreshPlanGird: function () {
        this.refreshPlanGrid();
    },

    onClearQC: function () {
        Ext.getCmp("editDispatcherQC").setValue(null);
        Ext.getCmp("editTruckCompanyQC").setValue(null);
        Ext.getCmp("editTruckNumberQC").setValue(null);
        Ext.getCmp("editDriverQC").setValue(null);
        Ext.getCmp("editPlanDT1QC").setValue(null);
        Ext.getCmp("editPlanDT2QC").setValue(null);
    }
});